package ReSofting.it.GoldDirector.Helpers;

import java.util.ArrayList;
import java.util.List;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import ReSofting.it.GoldDirector.Db.DataDbManager;
import ReSofting.it.GoldDirector.Interfaces.IRemoteCallCategorie;
import ReSofting.it.GoldDirector.Interfaces.IRemoteCallListener;
import ReSofting.it.GoldDirector.Model.GlobalDatas;
import ReSofting.it.GoldDirector.Model.ReturnData;
import ReSofting.it.GoldDirector.Model.myCategoria;
import ReSofting.it.GoldDirector.Model.myMenu;



public class ItemsLoader
	implements IRemoteCallListener<ReturnData< List<myMenu>> > ,
	IRemoteCallCategorie<ReturnData<List<myCategoria>>>  
{
	 	 
		ProgressDialog _progrDialog = null;
		Context _Context = null;		
		DataDbManager _dataMan = null;
	
 public ItemsLoader(Context pContext)
 {
	 _Context = pContext;
	 _dataMan = new DataDbManager(this._Context);
 }
	
	
 /*
  * Preleva gli items dal WCF , MAIN METHOD
  */
 public Boolean getItems()
 {
	 Log.i("In getItems", "Start");
	 getCategorieAsync();
	 return true;
 }
 
 public void CloseDb()
 {
	 _dataMan.CloseDb();
 }
 
 
	 private void getCategorieAsync()
		{		
			_progrDialog = ProgressDialog.show(_Context, "", "Loading Categorie menu. Please wait...", true);			
			
			//10.0.2.2:8096
			String url = 
				"http://"+ GlobalDatas.NetSettings.HostIp +":"+
				GlobalDatas.NetSettings.Port +
				"/Services/OrdersSvc.svc/GetCategorie";			
						
			//if(resVarie.isDebuggable(_Context))
				//url = "http://10.0.2.2:8096/Services/OrdersSvc.svc/GetCategorie";

			GetCategoriesAsync taskIN = new GetCategoriesAsync(this,_Context);
			taskIN.execute(url);
			
		}//fM
	 
	 private void getMenuAsync()
	{		
		_progrDialog = ProgressDialog.show(_Context, "", "Loading Piatti menu. Please wait...", true);
		
		//10.0.2.2:8096
		String url = 
			"http://"+ GlobalDatas.NetSettings.HostIp +":"+
			GlobalDatas.NetSettings.Port +
			"/Services/OrdersSvc.svc/GetMenu";
		
		
		//if(resVarie.isDebuggable(_Context))
			//url = "http://10.0.2.2:8096/Services/OrdersSvc.svc/GetMenu";

		Log.i("In getMenuAsync", "Start");
		
	    new GetDataMenuAsync(this).execute(url);
		
	}//fM
	
	public void showStatoDb()
	{
		 Log.d("In showStatoDb : ", "Start");
		 String datiDb = _dataMan.getInfoTabelleDatiDb();
		 
		 Log.i(" getItems End ", datiDb );
		 resVarie.showMessageDialog("Info Dati caricati in DB \r\n"+ datiDb, _Context);
		 
		 CloseDb();
		 	 
	}
	
	
	//legato al metodo getMenu() passata come interfaccia di callback
	 public void onRemoteCallComplete(ReturnData< List<myMenu>>   pDati) 
	 {
		 _progrDialog.cancel();
		 
		 try {
			 
			if(pDati.HasErrors)
			 {
				 if(pDati.StatusCode != 200)
			     {
					 resVarie.Alert2("Errore connessione rete"," Staus Code : "+ pDati.StatusCode, _Context);
			     }
			     
				 return ;
			 }
			 
			 Log.i("In onRemoteCallComplete", "Start");
			 
			 List<myMenu> lista = pDati.Dati;
			 
			 if(lista == null)
				 {
				 	Toast.makeText(_Context, "Errore, menu non scaricati", Toast.LENGTH_LONG );
				 	return;
				 }
			 
			 	Log.i("In onRemoteCallComplete ", "Tot menu scaricati : "+ lista.size() );
			    	        			    
			   caricaMenuItemsInDb(lista);
			   caricaMenuItemsInCache(lista);
			    
			   Log.d("In onRemoteCallComplete : ", "Items caricati OK");
			   
		} catch (Exception e) {
			 Log.d("ERROR In onRemoteCallComplete : ", "");
			e.printStackTrace();
		}
	        
		   showStatoDb();
	        	        
	    }//fM
	 
	 //chiama il carico delle categorie
	 public void onRemoteCallCategorieCompleted(ReturnData<List<myCategoria>> pDati) 
	 {
		 try {
			 
				if(pDati.HasErrors)
				 {
					 if(pDati.StatusCode != 200)
				     {
						 resVarie.Alert2("Errore connessione rete"," Staus Code : "+ pDati.StatusCode, _Context);
				     }				     
					 return ;
				 }
				 
				 Log.i("In onRemoteCallComplete", "Start");
				 
				 List<myCategoria> lista = pDati.Dati;				 
				 if(lista == null)
					 {
					 	Toast.makeText(_Context, "Errore, Categorie non scaricate", Toast.LENGTH_LONG );
					 	return;
					 }
				 
				 Log.i("In onRemoteCallComplete ", "Tot categorie scaricati : "+ lista.size() );
				    	        	 
	        loadCategoriesInDb(lista);
	        Log.d("Categorie caricate in DB : ", "OK");
	        
	        loadCategoriesInCache(lista);
	        //Log.d("Categorie caricate in Cache : ", "OK");
	        
		    
	        //poi preleva il MENUITEMS
	        getMenuAsync(); 
	        
	 	   
			} catch (Exception e) {
				Log.d("ERROR In onRemoteCallComplete : ", "");
				e.printStackTrace();
				
				Toast.makeText(this._Context,"Errore in onRemoteCallCategorieCompleted", Toast.LENGTH_LONG);
			}
	        
	        	        
	    }//fM


	private void loadCategoriesInCache(List<myCategoria> pDatiList) {
		
		GlobalDatas.CategorieList.clear();
		if(pDatiList == null) return ;
		
		 try 
		    {
		        for (int i = 0; i < pDatiList.size(); ++i) 
		        {	        					
		        	GlobalDatas.CategorieList.add( pDatiList.get(i));			
		        }        
		    } 
		    catch (Exception e) 
		    {
				e.printStackTrace();
				Log.d("Errore in loadCategories : ",e.getMessage() );
			}	   		    
		    
		    //prelevo il menu
		    Log.d("Categorie aggiunte OK : ","" );
	}
	
	
	private void loadCategoriesInDb(List<myCategoria> pDatiList)
	{
				
		if(pDatiList == null) return;
				
		 try 
	        {
			 _dataMan.addCategoriesInDb(pDatiList, true);   
	        } 
	        catch (Exception e) 
	        {
	        	Log.d("Error In loadCategoriesInDb ",e.getMessage() );
				e.printStackTrace();
			}	   		
	}//fM
	
	
	
	
	private void caricaMenuItemsInCache(List<myMenu> pDatiList)
	{
		GlobalDatas.MenuList.clear();
		
		if(pDatiList == null) return;
		
		 try 
	        {
		        for (int i = 0; i < pDatiList.size(); ++i) 
		        {	        					
		        	GlobalDatas.MenuList.add( pDatiList.get(i));	
		        	Log.d("In caricaListDati ",pDatiList.get(i).GuidIDMn + " , "+  pDatiList.get(i).FkCategoria );
		        }        
	        } 
	        catch (Exception e) 
	        {
	        	Log.d("Error In caricaMenuItemsInCache ",e.getMessage() );
				e.printStackTrace();
			}	   		
	}//fM
		

	private void caricaMenuItemsInDb(List<myMenu> pDatiList)
	{
		//DataDbManager dm = new DataDbManager(this._Context);
		
		if(pDatiList == null) return;
		
		
		 try 
	        {
			 _dataMan.addMenuItemsInDb(pDatiList, true);   
	        } 
	        catch (Exception e) 
	        {
	        	Log.d("Error In caricaMenuItemsInDb ",e.getMessage() );
				e.printStackTrace();
			}	   		
	}//fM
	
	
	public void caricaCategorieInCacheFromDb()
	{
		GlobalDatas.CategorieList.clear();
		List<myCategoria> datiList =  _dataMan.getCategoriesFromDb();
		
		
		if(datiList == null) return;
		
		 try 
	        {
		        for (int i = 0; i < datiList.size(); ++i) 
		        {	        					
		        	GlobalDatas.CategorieList.add( datiList.get(i));	
		        	Log.d("In caricaCategorieInCacheFromDb ",datiList.get(i).Nome + " , " );
		        }        
	        } 
	        catch (Exception e) 
	        {
	        	Log.d("Error In caricaCategorieInCacheFromDb ",e.getMessage() );
				e.printStackTrace();
			}	   		
	}//fM
	
	
	public void caricaMenuItemsInCacheFromDb()
	{
		GlobalDatas.MenuList.clear();
		List<myMenu> datiList =  _dataMan.getMenuItemsFromDb();
		
		
		if(datiList == null) return;
		
		 try 
	        {
		        for (int i = 0; i < datiList.size(); ++i) 
		        {	        					
		        	GlobalDatas.MenuList.add( datiList.get(i));	
		        	Log.d("In caricaMenuItemsInCacheFromDb ",datiList.get(i).GuidIDMn + " , "+  datiList.get(i).FkCategoria );
		        }        
	        } 
	        catch (Exception e) 
	        {
	        	Log.d("Error In caricaMenuItemsInCacheFromDb ",e.getMessage() );
				e.printStackTrace();
			}	   		
	}//fM
	
	
	
}//fC
